//
//  ViewController5.m
//  ProjectA
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewController5.h" //C
#import "SettingsViewController.h"
#import "ViewControllerForUser.h"
#import "ControllerViewForDenglu.h"
#import "ViewForCollectFooter.h"


@interface ViewController5 ()<UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, assign) BOOL flag1;
@property (nonatomic, assign) BOOL flag2;
@property (nonatomic, strong) UIView *viewForShadow;    //头部可拉伸的image的覆盖图
@property (nonatomic, strong) LWHImageView *imageView;   //头部可拉伸的image

// 记录scrollView.contentInset.top(重点参数)
@property (nonatomic, assign)CGFloat marginTop;

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController5

- (void)dealloc {
    
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_viewForShadow release];
    [_imageView release];
    [_tableView release];
    [super dealloc];
    
}

- (void)setLightDayModel {
    [super setLightDayModel];
    
    self.navigationItem.rightBarButtonItem.tintColor = L_COLOR_WHITE;
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
    self.imageView.labelForName.textColor = L_COLOR_WHITE;
}

- (void)setNightDayModel {
    [super setNightDayModel];
    
    self.navigationItem.rightBarButtonItem.tintColor = L_COLOR_WHITE;
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
    self.imageView.labelForName.textColor = L_COLOR_WHITE;
}
- (void)viewWillAppear:(BOOL)animated {
    
    NSLog(@"%@", NSHomeDirectory());
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"个人封面"]) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"个人封面"];
        UIImage *image = [UIImage imageWithData:data];
        self.imageView.image = image;
    } else {
        self.imageView.image = [UIImage imageNamed:@"iconfont-jingse"];
    }
    
    
//    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"登录"]) {
//        NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"登录"];
//       
//        [self.imageView.imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"iconURL"]]];
//        
//        self.imageView.labelForName.text = [dic objectForKey:@"name"];
//    } else {
//        self.imageView.imageView.image = [UIImage imageNamed:@"iconfont-xiangji"];
//        self.imageView.labelForName.text = @"";
//    }
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
    
 
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
}
- (void)viewDidLoad {
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    
    [self settingsNavigationController];
    
    [self createTableView];
    
}


- (void)settingsNavigationController {
  
    self.navigationItem.title = @"";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shezhi"] style:UIBarButtonItemStylePlain target:self action:@selector(settingsAction:)] autorelease];

}

// 设置界面跳转方法
- (void)settingsAction:(UIBarButtonItem *)item {
    
    SettingsViewController *settings = [[SettingsViewController alloc] init];
    [self.navigationController pushViewController:settings animated:YES];
    self.tabBarController.tabBar.hidden = YES;
    [settings release];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[ViewForCollectFooter class] forHeaderFooterViewReuseIdentifier:@"poolForFooter"];
    [self createImageView];
    
}
- (void)createImageView {
    
    self.imageView = [[LWHImageView alloc] initWithFrame:CGRectMake(0, -200, [UIScreen mainScreen].bounds.size.width, 200)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.tag = 101;
    self.imageView.alpha = 0.7;
    [self.tableView addSubview:self.imageView];
    [_imageView release];
    self.imageView.userInteractionEnabled = YES;
    
//    [self.imageView.buttonForIcon addTarget:self action:@selector(buttonForAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    [self.imageView addGestureRecognizer:tap1];
    [tap1 release];
    
    

}
// 点击头像跳转个人信息界面
- (void)buttonForAction:(UIButton *)button {
    
//    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"登录"]) {   //如果登陆跳转个人中心界面
//            ViewControllerForUser *user = [[ViewControllerForUser alloc] init];
//            self.tabBarController.tabBar.hidden = YES;
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:user animated:YES];
//            [user release];
//            self.hidesBottomBarWhenPushed = NO;
//    } else {
//        ControllerViewForDenglu *sdp = [[ControllerViewForDenglu alloc] init];
//        [self.navigationController presentViewController:sdp animated:YES completion:^{
//      
//        }];
//        [sdp release];
//    }
}
// 更换个人封面
- (void)tap1Action:(UITapGestureRecognizer *)tap1 {
    self.flag1 = YES;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新个人封面" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    // 判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"支持相机");
    } else {
        NSLog(@"不支持相机");
    }
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        // 获取相册对象
        UIImagePickerController *imagePiker = [[[UIImagePickerController alloc] init] autorelease];
        
        
        //选择完成图片或者点击取消按钮都是通过代理来操作我们所需要的逻辑过程
        imagePiker.delegate = self;
        imagePiker.allowsEditing = YES; //相册对象是否可编辑
        
        
        //设置相册呈现的样式
        imagePiker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  //资源类型:相册
        

        
        //使用模态呈现相册
        [self presentViewController:imagePiker animated:YES completion:^{}];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action3];
    [alert addAction:action2];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

// 选择完照片调用该方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    //点击取消按钮所执行的方法
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    if (self.flag1) {
        
        // 获取照片
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        
        // 设置显示照片
        self.imageView.image = image;
        
        CGSize size = CGSizeMake(self.view.bounds.size.width, 200);
        
        //调整image的大小
        UIGraphicsBeginImageContextWithOptions(size, NO,0.0);
        CGRect imageRect=CGRectMake(0.0, 0.0, size.width, size.height);
        [image drawInRect:imageRect];
        self.imageView.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        // 保存图片到本地,  注意: 上传照片到服务器使用(暂未实现)
        NSData *imageData = UIImageJPEGRepresentation(image, 1);    //1为不缩放保存, 取值(0.0-1)
        
        [[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"个人封面"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.flag1 = NO;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    ViewForCollectFooter *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForFooter"];
    view.label.text = @"暂未有新功能";
    return view;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 300;
}

/** 背景照片滑动 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    if (self.marginTop != scrollView.contentInset.top) {
        self.marginTop = scrollView.contentInset.top;
    }
    
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat newoffsetY = offsetY + self.marginTop;
    

    if (newoffsetY >= 0 && newoffsetY <= 200) {
        
        self.barBackgroundView.alpha = newoffsetY / 200.f;
    } else if (newoffsetY > 200){
        self.barBackgroundView.alpha = 1;
    } else {
        self.barBackgroundView.alpha = 0;
    }
    
    // 下拉放大
    CGPoint point = scrollView.contentOffset;
    
    if (point.y < -200) {
        CGRect rect = [self.tableView viewWithTag:101].frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        [self.tableView viewWithTag:101].frame = rect;
     //   [self.tableView viewWithTag:102].frame = rect;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
